package com.jtjrenren.android.taxi.passenger.core.app.splash;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Message;

import com.jtjrenren.android.taxi.passenger.core.app.guide.AppGuideActivity;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.core.main.HomeActivity;
import com.jtjrenren.android.taxi.passenger.utils.UIHelper;
import com.library.weidget.CustomeDialog;
import com.jtjrenren.android.taxi.passenger.base.BaseMvpActivity;
import com.library.utils.AppUtils;

import java.io.File;

/**
 * Created by Administrator on 2015/12/21.
 */
public class AppSplashActivity extends BaseMvpActivity<SplashPresenter> implements SplashView {
    private ProgressDialog progressDialog;
    private long start;

    @Override
    public int getLayoutId() {
        return R.layout.aty_app_splash;
    }

    @Override
    public void initM() {
        initHandler();
        start = System.currentTimeMillis();
        presenter.onSub();
        presenter.initAPP();
    }

    @Override
    protected void handMsg(Message msg) {
        super.handMsg(msg);
        switch (msg.what){
            case 1:
                showMainPage();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unSub();
    }

    @Override
    public void showGuidePage() {
        startActivity(new Intent(AppSplashActivity.this, AppGuideActivity.class));
        finish();
    }

    @Override
    public void showMainPage() {
        if (System.currentTimeMillis() - start < 3000) {
            handler.sendEmptyMessageDelayed(1, 3000 - (System.currentTimeMillis() - start));
            return;
        }
        Intent intent = new Intent(AppSplashActivity.this, HomeActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void refreshDownProcess(int process) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("正在下载最新apk");
            progressDialog.setMax(100);
            progressDialog.show();
        }
        progressDialog.setProgress(process);
    }

    @Override
    public void downFinish(String path) {
        AppUtils.installApk(new File(path), this);
    }

    @Override
    public void downError(String msg) {

    }


    @Override
    public SplashPresenterImpl createPresenter() {
        return new SplashPresenterImpl(this);
    }

    @Override
    public void showUpDialog(final String url, String content, final boolean b) {
        String title = getResources().getString(R.string.upapp);
        if (b)
            title = getResources().getString(R.string.upapp_force);

        CustomeDialog dialog = UIHelper.getCustomeDialog(this, title, content, getString(R.string.update_now),
                getString(R.string.cancle_update),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        presenter.downApp(url);
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (!b) {
                            dialogInterface.dismiss();
                            presenter.doUpNext();
                        }
                    }
                });

        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }
}
